package com.meida.module.push.provider.configuration;

import cn.jpush.api.JPushClient;
import com.meida.common.mybatis.entity.EntityMap;
import com.meida.common.utils.FlymeUtils;
import com.meida.common.utils.RedisUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * 默认配置类
 *
 * @author zyf
 */
@Slf4j
@Configuration
public class PushConfiguration {


    @Bean
    @ConditionalOnMissingBean(JPushClient.class)
    public JPushClient jPushClient(RedisUtils redisUtils) {
        JPushClient jPushClient = null;
        EntityMap map = redisUtils.getConfigMap("PUSH_CONFIG");
        String serviceName = map.get("serviceName");
        if (FlymeUtils.isNotEmpty(serviceName) && serviceName.equals("PUSH_JG")) {
            String appKey = map.get("appKey");
            String secretKey = map.get("secretKey");
            if (FlymeUtils.allNotNull(appKey, secretKey)) {
                try {
                    jPushClient = new JPushClient(secretKey, appKey);
                } catch (Exception e) {
                    log.error("jPushClient初始化失败");
                }
            }
        }
        return jPushClient;
    }
}
